package com.chushouya.controller.order.admin;


import com.chushouya.common.annotations.AuthValidate;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.order.dto.admin.action.*;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.enums.LimitType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RateLimiter;
import com.chushouya.order.constants.KefuOrderActionEnum;
import com.chushouya.order.service.admin.KefuOrderActionCtrlService;
import com.chushouya.order.service.admin.KefuOrderActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = ServiceType.PLATFORM + "平台订单操作接口")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/orderAction")
public class OrderActionController {

    @Resource
    private KefuOrderActionCtrlService kefuOrderActionCtrlService;

    @Resource
    private KefuOrderActionService kefuOrderActionService;

    @GetMapping("/orderActions")
    @ApiOperation("订单操作")
    @AuthValidate
    public Response<List<OrderAction>> orderActions(Long orderId) {
        List<OrderAction> orderActions = kefuOrderActionCtrlService.orderActions(orderId);
        orderActions.removeIf(action -> action.getActionCode().equals(KefuOrderActionEnum.QUALITY.value()));
        return Response.success(orderActions);
    }

    @ApiOperation("待跟进接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/waitOrder")
    public Response<Void> waitOrder(@RequestBody @Valid OrderWaitAction dto) {
        kefuOrderActionService.waitOrder(dto);
        return Response.success();
    }

    @ApiOperation("关闭订单接口")
    @Log(businessType = BusinessType.UPDATE)
    @RateLimiter(time = 5, count = 1, limitType = LimitType.IP)
    @PostMapping("/closeOrder")
    public Response<Void> closeOrder(@RequestBody @Valid OrderCloseAction dto) {
        kefuOrderActionService.closeOrder(dto);
        return Response.success();
    }

    @ApiOperation("订单完成接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/finishOrder")
    public Response<Void> finishOrder(@RequestBody @Valid OrderCompleteAction dto) {
        kefuOrderActionService.orderComplete(dto);
        return Response.success();
    }

    @ApiOperation("订单派单接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/dispatchOrder")
    public Response<Void> dispatchOrder(@RequestBody @Valid OrderDispatchAction dto) {
        kefuOrderActionService.orderDispatch(dto);
        return Response.success();
    }

    @ApiOperation("订单待质检接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderReceive")
    public Response<Void> orderReceive(@RequestBody @Valid OrderReceiveAction dto) {
        kefuOrderActionService.orderReceive(dto);
        return Response.success();
    }

    @ApiOperation("完成议价接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderBargain")
    public Response<Void> orderBargain(@RequestBody @Valid OrderBargainAction dto) {
        kefuOrderActionService.orderBargain(dto);
        return Response.success();
    }

    @ApiOperation("回滚到待议价接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/rollbackToBargain")
    public Response<Void> rollbackToWaitOrder(@RequestBody @Valid OrderReBargainAction dto) {
        kefuOrderActionService.orderReBargain(dto);
        return Response.success();
    }

    @ApiOperation("再开一单接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderAgain")
    public Response<Long> orderAgain(@RequestBody @Valid OrderAgainAction dto) {

        return Response.success(kefuOrderActionService.orderAgain(dto));
    }

    @ApiOperation("保存用户收款信息接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/submitCollection")
    public Response<Void> saveCollection(@RequestBody @Valid SubmitCollectionAction dto) {
//        kefuOrderActionService.submitCollection(dto);
        return Response.success();
    }

    @ApiOperation("重置订单状态接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/resetOrderStatus")
    public Response<Void> resetOrderStatus(@RequestBody @Valid ResetOrderStatusAction dto) {
        kefuOrderActionService.resetOrderStatus(dto);
        return Response.success();
    }

    @ApiOperation("订单退货接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderReturn")
    public Response<Void> orderReturn(@RequestBody @Valid OrderReturnAction dto) {
        kefuOrderActionService.orderReturn(dto);
        return Response.success();
    }

    @ApiOperation("订单预约快递接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderDeliver")
    public Response<Void> orderDeliver(@RequestBody @Valid OrderDeliverAction dto) {
        kefuOrderActionService.orderDeliver(dto);
        return Response.success();
    }

    @ApiOperation("订单发货接口")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/orderDelivery")
    public Response<Void> orderDelivery(@RequestBody @Valid OrderDeliveryAction dto) {
        kefuOrderActionService.deliveryOrder(dto);
        return Response.success();
    }

    @ApiOperation("待入库：业务员已售出")
    @PostMapping("/clerkSaleOut")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> clerkSaleOut(@RequestBody OrderClerkSaleOutAction dto) {
        kefuOrderActionService.clerkSaleOut(dto);
        return Response.success();
    }

    @ApiOperation("订单重发能量")
    @PostMapping("/orderSendEnergy")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> orderSendEnergy(@RequestBody OrderSendEnergyAction dto) {
        kefuOrderActionService.orderSendEnergy(dto);
        return Response.success();
    }

    @ApiOperation("订单质检")
    @PostMapping("/orderQuality")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> orderQuality(@RequestBody OrderQualityAction dto) {
        kefuOrderActionService.orderQuality(dto);
        return Response.success();
    }

    @ApiOperation("订单完成支付接口")
    @PostMapping("/orderCompletePay")
    @Log(businessType = BusinessType.UPDATE)
    @RateLimiter(time = 10, count = 1, limitType = LimitType.IP)
    public Response<Void> orderCompletePay(@RequestBody @Valid OrderCompletePayAction dto) {
        kefuOrderActionService.orderCompletePay(dto);
        return Response.success();
    }
}
